package com.ecfront.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ecfront.dao.UserDAO;
import com.ecfront.pojo.User;


/**
 * @version 1.0
 * @author www.ecfrom.com
 */

public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
	
	protected void initDao() {}


	public void save(User transientInstance) {
		getHibernateTemplate().saveOrUpdate(transientInstance);
	}

	public void delete(User persistentInstance) {
		getHibernateTemplate().delete(persistentInstance);
	}

	public User findById(java.lang.Integer id) {
		User instance = (User) getHibernateTemplate().get(
					"com.ecfront.pojo.User", id);
		return instance;
	}

	public List findByExample(User instance) {
		List results = getHibernateTemplate().findByExample(instance);
		return results;
	}

	public List findByProperty(String propertyName, Object value) {
		String queryString = "from User as model where model."
					+ propertyName + "= ?";
		return getHibernateTemplate().find(queryString, value);
	}
	
	public boolean findUserByNameOrEmailAndPassAndLock(String nameOrEmail,
			String loginPwd, int isLock) {
		List ul = getHibernateTemplate()
				.find(
						"from User t where (t.name = ? or t.email = ?) and t.loginPwd=? and t.isLock=?",
						new Object[] { nameOrEmail, nameOrEmail, loginPwd,isLock });
		if (ul != null && ul.size() >= 1) {
			return true;
		}
		return false;
	}

	public User findByUname(Object uname) {
		List list = findByProperty(UNAME, uname);
		if(list!=null&&list.size()>=1){
			return (User)list.get(0);
		}
		return null;
	}

	public User findByUemail(Object uemail) {
		List list = findByProperty(UEMAIL, uemail);
		if(list!=null&&list.size()>=1){
			return (User)list.get(0);
		}
		return null;
	}

	public List findByUloginPassword(Object uloginPassword) {
		return findByProperty(ULOGIN_PASSWORD, uloginPassword);
	}
	public List findByUpayPassword(Object upayPassword) {
		return findByProperty(UPAY_PASSWORD, upayPassword);
	}

	public List findByUquestion(Object uquestion) {
		return findByProperty(UQUESTION, uquestion);
	}
	public List findByUanswer(Object uanswer) {
		return findByProperty(UANSWER, uanswer);
	}

	public List findByUrealName(Object urealName) {
		return findByProperty(UREAL_NAME, urealName);
	}

	public List findByUsex(Object usex) {
		return findByProperty(USEX, usex);
	}

	public List findByUage(Object uage) {
		return findByProperty(UAGE, uage);
	}


	public List findByUphone(Object uphone) {
		return findByProperty(UPHONE, uphone);
	}

	public List findByUqq(Object uqq) {
		return findByProperty(UQQ, uqq);
	}

	public List findByUmsn(Object umsn) {
		return findByProperty(UMSN, umsn);
	}

	public List findByUwangWang(Object uwangWang) {
		return findByProperty(UWANG_WANG, uwangWang);
	}

	public List findByUzipCode(Object uzipCode) {
		return findByProperty(UZIP_CODE, uzipCode);
	}

	public List findByUadd(Object uadd) {
		return findByProperty(UADD, uadd);
	}

	public List findByUmobilePhome(Object umobilePhome) {
		return findByProperty(UMOBILE_PHOME, umobilePhome);
	}

	public List findByUic(Object uic) {
		return findByProperty(UIC, uic);
	}

	public List findByUphotoUrl(Object uphotoUrl) {
		return findByProperty(UPHOTO_URL, uphotoUrl);
	}

	public List findByUmoney(Object umoney) {
		return findByProperty(UMONEY, umoney);
	}

	public List findByUbuyScore(Object ubuyScore) {
		return findByProperty(UBUY_SCORE, ubuyScore);
	}

	public List findByUwam(Object uwam) {
		return findByProperty(UWAM, uwam);
	}


	public List findByUpayBank(Object upayBank) {
		return findByProperty(UPAY_BANK, upayBank);
	}

	public List findByUpayNbr(Object upayNbr) {
		return findByProperty(UPAY_NBR, upayNbr);
	}

	public List findByUisLock(Object uisLock) {
		return findByProperty(UIS_LOCK, uisLock);
	}

	public List findAll() {
		String queryString = "from User";
		return getHibernateTemplate().find(queryString);
	}


	public User merge(User detachedInstance) {
		User result = (User) getHibernateTemplate().merge(detachedInstance);
		return result;
	}
	public void attachDirty(User instance) {
		getHibernateTemplate().saveOrUpdate(instance);
	}

	public void attachClean(User instance) {
		getHibernateTemplate().lock(instance, LockMode.NONE);
	}

	public static UserDAO getFromApplicationContext(ApplicationContext ctx) {
		return (UserDAO) ctx.getBean("UserDAO");
	}
}